﻿using Com.Lancetinc.Evs.Reports;
using Com.Lancetinc.Evs.ViewModels;
using Com.Lancetinc.Evs.Windows;
using Prism.Commands;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 报告常用术语
    /// </summary>
    public partial class ReportTermsUserControl
    {
        public ReportTermsUserViewModel ViewModel { get; }

        public event EventHandler<string> TermSelected;

        public ReportTermsUserControl()
        {
            InitializeComponent();
            ViewModel = new ReportTermsUserViewModel();
            this.DataContext = ViewModel;
        }

        private void LblTerm_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            // 双击
            if (e.ClickCount == 2)
            {
                var tag = (sender as FrameworkElement)?.Tag;
                var term = tag?.ToString();
                if (string.IsNullOrEmpty(term)) return;

                TermSelected?.Invoke(this, term);
            }
        }
    }

    public class ReportTermsUserViewModel : ViewModelBase
    {
        public ReportTerminology[] Terms { get; set; }

        public ICommand EditCommand { get; }

        public ReportTermsUserViewModel()
        {
            EditCommand = new DelegateCommand(ExecuteEditCommand);
            LoadTerms();
        }

        public void LoadTerms()
        {
            Terms = ReportHelper.LoadReportTerminologies().ToArray();
            RaisePropertyChanged(nameof(Terms));
        }

        private void ExecuteEditCommand()
        {
            var win = new TerminologyEditWindow
            {
                Owner = Application.Current.MainWindow
            };

            if (win.ShowDialog() != true)
                return;

            LoadTerms();
        }
    }
}